package com.nuttysoft.fangchan.scrolllistview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import java.util.logging.Logger;

/**
 * Created by meijun on 2016/6/22.
 * email:hellomeijun@gmail.com
 */
public class MyListView extends ListView implements AbsListView.OnScrollListener {

    //-1down 1 up
    static int current = 0;

    int lastitem;
    int lasttop;

    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnScrollListener(this);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        Log.e("mainactivity", "scrollstate = " + scrollState);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  /*      Log.e("mainactivity","firstvisialeposition  = " + this.getFirstVisiblePosition());
        Log.e("mainactivity","fisrt = "+ firstVisibleItem +" visibleitemCount =" +visibleItemCount+" totalItemcount = "+totalItemCount  );
*/
        if (firstVisibleItem - lastitem > 0) {
            //往下
            //  Log.e("mainactivity","往下 ");
            if (mListener != null && current != -1) {
                mListener.down();
                current = -1;
                Log.e("mainactivity2", "222222往下 ");
            }
            lasttop = 0;
        } else if (firstVisibleItem - lastitem < 0) {
            //往上
            //   Log.e("mainactivity3","往上 ");
            if (mListener != null && current == -1) {
                mListener.up();
                current = 1;
                Log.e("mainactivity4", "-2-2-2-2-2往上 ");
            }
            View childAt = getChildAt(0);
            if (childAt != null) {
                lasttop = childAt.getTop();
            } else {
                return;
            }
        }
        //当在同一行时
        else if (firstVisibleItem == lastitem) {
            View childAt = getChildAt(0);
            int top = 0;
            if (childAt != null) {
                top = childAt.getTop();
            } else {
                return;
            }
            Log.e("mainactivity", "top = " + top + "lasttop = " + lasttop);
            if (top - lasttop < 0) {
                // Log.e("MainActivity","00000000000往下");
                if (mListener != null && current != -1) {
                    mListener.down();
                    current = -1;
                    Log.e("main1", "-1-1-1-1--1-1");
                }
                //  Log.e("main2","-1-1-1-1--1-1");
            } else if (top - lasttop > 0) {
                if (mListener != null && current != 1) {
                    mListener.up();
                    current = 1;
                    Log.e("main3", "111111");
                }
            }

            lasttop = top;
        }
        lastitem = firstVisibleItem;

    }

    interface OnScrollListener {

        /**
         * 当手指向上滑动时将回调此方法
         */
        public void up();

        /**
         * 当手指往下h滑动时将回调此方法
         */
        public void down();
    }

    OnScrollListener mListener;

    public void setOnScrollListener(OnScrollListener listener) {
        this.mListener = listener;
    }


}
